package com.thinkgem.jeesite.modules.KDniao.unit;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 规定两个javabean必须是一个实体javabean，值不相同。
 * 
 * @author acer
 * @param <S>复制后得到对象
 * @param <D>被复制对象
 * 
 */
public class BeanMapper<S> {
    public synchronized boolean jums(S s, S d) {
        Method methodd = null;
        Method methods = null;
            try {
                methodd = d.getClass().getMethod("getId", new Class[] {});
                methods = s.getClass().getMethod("getId", new Class[] {});
            } catch (NoSuchMethodException e) {
                e.addSuppressed(new Exception("没有gitId这个方法"));
            } catch (SecurityException e) {
                e.addSuppressed(new Exception("没有gitId这个方法"));
            }
        Object obj = null;
        Object object = null;
            try {
                obj = methodd.invoke(d, new Object[] {});
            object = methods.invoke(s, new Object[] {});
            } catch (IllegalAccessException e) {
            e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (obj != null && object != null && !"".equals(object) && !"".equals(obj)) {
            return obj.equals(object);
        }
        return false;
    }

    public synchronized String toString(S s) {
        Field[] fields = s.getClass().getDeclaredFields();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldNames = field.getName();
            String getMethodNames = "get" + fieldNames.substring(0, 1).toUpperCase() + fieldNames.substring(1);
            Method methodd = null;
            if ("serialVersionUID".equals(fieldNames)) {
                continue;
            }
            try {
                methodd = s.getClass().getMethod(getMethodNames, new Class[] {});
            } catch (NoSuchMethodException e) {
                //当前方法不是标准的javabean的get方法是有可能是字母小写
                getMethodNames = "get" + fieldNames.substring(0, 1) + fieldNames.substring(1);
                try {
                    methodd = s.getClass().getMethod(getMethodNames, new Class[] {});
                } catch (NoSuchMethodException e1) {
                    System.out.println("无法判定当前方法，因为不是一个javabean标准方法");
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Object obj = null;
            try {
                obj = methodd.invoke(s, new Object[] {});
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            builder.append(fieldNames + "==" + obj + "  ");
        }
        return builder.toString();
    }
}
